<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>找回密码</title>
    <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="/public/css/login.css">
    <style>
    .col-sm-8 {
        padding-left:0;
    }
    </style>
</head>
<body>
    <div class="container main">
        <div class="col-sm-4 col-xs-12">
            <div class="header">
                <a href="/">
                <img src="/public/img/logo.png" alt=""></a>
            </div>
            <h1>找回密码</h1>
            <form id="forgetPwd_form">
                <div class="form-group">
                    <input type="email" class="form-control" id="email" placeholder="请输入QQ邮箱" name="email" autofocus required>
                    <span id="tipOne" class="redColor"></span>
                </div>
                <div class="form-group" style="overflow: hidden;">
                    <div class="col-sm-8 col-xs-8">
                        <input type="text" class="form-control" id="code" placeholder="请输入验证码" required>
                    </div>
                    <a href="javascript:;" class="btn btn-success col-sm-4 col-xs-4" id="codeBtn">获取验证码</a>
                    <span id="tipTwo" class="redColor"></span>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="password" placeholder="请输入密码" name="password" required>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="comfirmPwd" placeholder="请确认密码" required>
                    <span id="tipThree" class="redColor"></span>
                </div>
                <button type="submit" class="btn btn-success btn-block">提交</button>
            </form>
            <div class="message">
                <p><i class="layui-icon layui-icon-return"></i><a href="/login"> 返回登录</a></p>
            </div>
        </div>
    </div>
    
    <script src="/node_modules/jquery/dist/jquery.js"></script>
    <script>
    $(function () {
        $('#codeBtn').on('click', function () {
            if ($('#email').val() == '' || $('#email').val() == null) {
                $('#tipOne').text('请先输入QQ邮箱，再获取验证码')
            } else {
                $.ajax({
                    url: '/getCodeAndSendMail/' + $('#email').val(),
                    type: 'get',
                    success: function (data) {
                        var err_code = data.err_code
                        if (err_code === 1) {
                            $('#tipOne').text(data.message)
                        } else if (err_code === 0) {
                            $('#code').on('blur', function () {
                                if ($(this).val() != data.code) {
                                    $('#tipTwo').text('验证码不正确，请重新输入')
                                } else {
                                    $("#comfirmPwd").on('blur', function () {
                                        if ($('#password').val() != $(this).val()) {
                                            $('#tipThree').text('两次密码不一致，请重新输入')
                                            $(this).focus()
                                        } else {
                                            $('#tipThree').text('密码一致')
                                            $('#forgetPwd_form').on('submit', function (e) {
                                                e.preventDefault()
                                                var formData = $(this).serialize()
                                                $.ajax({
                                                    url: '/updateNewPwd',
                                                    type: 'post',
                                                    data: formData,
                                                    dataType: 'json',
                                                    success: function (data) {
                                                        var err_code = data.err_code
                                                        if (err_code === 0) {
                                                            window.location.href = '/login'
                                                        }
                                                    }
                                                })
                                            })
                                        }
                                    })
                                }
                            })
                        } else if (err_code === -1) {
                            $('#tipTwo').text(data.message)
                        }
                    }
                })
            }
        })
    })
    </script>
</body>
</html>